/*
 * Copyright (c) 2012, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.bmi.gwt.mi.server.channel;

import java.util.HashMap;
import java.util.regex.Pattern;

import javassist.NotFoundException;

import org.bmi.gwt.mi.server.core.queue.MiQueue;
import org.bmi.gwt.mi.server.core.queue.QueuesManager;
import org.bmi.gwt.mi.server.impl.ProxyFactoryServer;
import org.bmi.gwt.mi.server.impl.WrapperFactoryServer;
import org.bmi.gwt.mi.shared.BroadChannel;
import org.bmi.gwt.mi.shared.Channel;
import org.bmi.gwt.mi.shared.exceptions.NameParseException;
import org.bmi.gwt.mi.shared.util.Constants;
import org.bmi.gwt.mi.shared.util.ProxyFactory;
import org.bmi.gwt.mi.shared.util.WrapperFactory;

public class ChannelFactory
{
	private QueuesManager queuesManager;

	private ProxyFactory proxyFactory;
	private WrapperFactory wrapperFactory;

	public ChannelFactory() throws NotFoundException
	{
		this.wrapperFactory = new WrapperFactoryServer();
		this.proxyFactory = new ProxyFactoryServer();
	}

	public void setQueuesManager(QueuesManager queuesManager)
	{
		this.queuesManager = queuesManager;
	}

	/**
	 * Returns an object of type {@link BroadChannel}.
	 * 
	 * @param queueExpr The expression defining the pool.
	 * @return
	 * @throws NameParseException
	 */
	public BroadChannel getBroadChannel(String queueExpr) throws NameParseException
	{
		if (!Pattern.matches("((\\w+)\\.)*((\\w+)|\\*)", queueExpr))
			throw new NameParseException("Invalid queue expression: " + queueExpr);

		return new BroadChannelServer(queueExpr, this.queuesManager, this.proxyFactory);
	}

	public Channel getChannel(String queueName) throws NameParseException
	{
		if (!Pattern.matches("((\\w+)\\.)*(\\w+)", queueName))
			throw new NameParseException("Invalid queue name: " + queueName);

		return new ChannelServer(queueName, this.queuesManager, this.proxyFactory, this.wrapperFactory,
				Constants.SERVER_CREDENTIALS);
	}

	public Channel getChannelNoBind(String queueName)
	{
		return new ChannelServerNoBind(queueName, this.queuesManager, this.proxyFactory, this.wrapperFactory);
	}

	public boolean isQueueAlive(String queueName) throws NameParseException
	{
		if (!Pattern.matches("((\\w+)\\.)*(\\w+)", queueName))
			throw new NameParseException("Invalid queue name: " + queueName);

		return this.queuesManager.isQueueAlive(queueName);
	}

	public HashMap<String, Object> getQueueSession(String queueName)
	{
		MiQueue queue = this.queuesManager.tryQueue(queueName);

		return queue == null ? null : queue.getSession();
	}
}
